<?php

class Login extends Controller {
	
	function Login()
	{
		parent::Controller();
		$this->load->model('Member_model');
	}
	
	function index() {
		//--get value from form Login
		$ary_info = array(
			'email' => $this->input->post('tf_email'),
			'password' => dohash($this->input->post('tf_password')),	
		);
							
		if ($this->input->post('btn_login')) {
			$result = $this->Member_model->getMember($ary_info);
			if (count($result)>0) {
				//--get profile if member type is par
				if ($result['member_type']==1) {
					$member_info = $this->Member_model->getMember_par(array('mem.id'=>$result['id']));
					$this->session->set_userdata(array(
						'ss_member_id'=>$member_info['id'],
						'ss_firstname'=>$member_info['firstname'],
						'ss_lastname'=>$member_info['lastname'],
						'ss_member_type'=>$member_info['member_type']
					));
				} else {//--if account type is pro
					$member_info = $this->Member_model->getMember_pro(array('mem.id'=>$result['id']));
					$this->session->set_userdata(array(
						'ss_member_id'=>$member_info['id'],
						'ss_member_type'=>$member_info['member_type']
					));
				}
				
				$this->session->set_userdata(array(
					'ss_email'=>$result['email'],
				));
				redirect('');
			}
		}
		
		$data['inc_script'] = '';
		$this->load->view('login_view',$data);
	}
	
	function ajax_login() {
		//--get value from form Login
		$ary_info = array(
			'email' => $this->input->post('tf_signin_email'),
			'password' => dohash($this->input->post('tf_signin_password')),	
		);	
		
		$result = $this->Member_model->getMember($ary_info);
		if (count($result)>0) {
			//--get profile if member type is par
			if ($result['member_type']==1) {
				$member_info = $this->Member_model->getMember_par(array('mem.id'=>$result['id']));
				$this->session->set_userdata(array(
					'ss_member_id'=>$member_info['id'],
					'ss_firstname'=>$member_info['firstname'],
					'ss_lastname'=>$member_info['lastname'],
					'ss_member_type'=>$member_info['member_type']
				));
			} else {//--if account type is pro
				$member_info = $this->Member_model->getMember_pro(array('mem.id'=>$result['id']));
				$this->session->set_userdata(array(
					'ss_member_id'=>$member_info['id'],
					'ss_member_type'=>$member_info['member_type']
				));
			}
			
			$this->session->set_userdata(array(
				'ss_email'=>$result['email'],
			));
			
			echo "true";
		} else {
			echo "false";
		}
	}
}